/*
 * Copyright (C) 2015 truth
 *
 * This program is non-free software, but you can redistribute it and/or modify
 * it if you are in hjj(Huangshi Hejianjiao) team.
 */
package com.truthbean.jutils.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseController extends HttpServlet {

	/**
	 * @Field serialVersionUID: 序列化
	 * @FieldType: long
	 * @Since: 2015年4月26日 上午7:02:42
	 * @Author: truth
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String GET = "/get";
	private static final String POST = "/post";
	private static final String DELETE = "/delete";
	private static final String PUT = "/put";
	
	private static final String DELETE_METHOD = "_DELETE";
	private static final String PUT_METHOD = "_PUT";
	private static final String GET_METHOD = "_GET";
	private static final String POST_METHOD = "_POST";

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String info = request.getPathInfo();
		System.out.println(info);
		if(POST.equals(info)){
			this.doPost(request, response);
		}else if(DELETE.equals(info)){
			this.doDelete(request, response);
		}else if(PUT.equals(info)){
			this.doPut(request, response);
		}else if(GET.equals(info)){
			this.doQueryAction(request, response);
		}else if(info!=null && !info.isEmpty()){
			if(info.contains(POST+"/"))
				this.doPost(request, response);
			if(info.contains(DELETE+"/"))
				this.doDelete(request, response);
			if(info.contains(GET+"/"))
				this.doQueryAction(request, response);
			if(info.contains(PUT+"/"))
				this.doPut(request, response);
			else
				doGetAction(request, response);
		}
	}
	
	protected abstract void doGetAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
	
	protected abstract void doQueryAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String _method = request.getParameter("_METHOD");
		if(DELETE_METHOD.equals(_method)){
			this.doDelete(request, response);
		}else if(PUT_METHOD.equals(_method)){
			this.doPut(request, response);
		}else if(GET_METHOD.equals(_method)){
			this.doQueryAction(request, response);
		}else if(POST_METHOD.equals(_method)){
			this.doInsertAction(request, response);
		}else{
			doPostAction(request, response);
		}
	}
	
	protected abstract void doPostAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
	
	protected abstract void doInsertAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

	@Override
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doUpdateAction(request, response);
	}
	
	protected abstract void doUpdateAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

	@Override
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doDeleteAction(request, response);
	}
	
	protected abstract void doDeleteAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

}
